import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * excel答题卡自动阅卷程序
 */

public class main {
    public static void main(String[] args) throws Exception {
        ImportExecl poi = new ImportExecl();
        System.out.println("请将答案文件路径拖动到下一行，然后按回车....");
        Scanner scanner1=new Scanner(System.in);
        String KeyPath=scanner1.next();
        System.out.println("正在读取答案....");
        List<List<String>> list = poi.read(KeyPath);  //答案路径
        List<String> right_ans = new ArrayList<String>();
        if (list != null) {
            for (int i = 5; i <list.size(); i++) {
                List<String> cellList = list.get(i);
                for (int j = 1; j < 2; j++) {
                    right_ans.add(cellList.get(1));  //将答案放在List中
                }
            }
        }
        System.out.println("请输入班级....");
        Scanner scanner2 = new Scanner(System.in);
        String a = scanner2.nextLine();
        String excelPath = "Report-" + a + ".xls";
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet(a);
        Row row0 = sheet.createRow(0); //创建第一行
        Cell cell_1 = row0.createCell(0, Cell.CELL_TYPE_STRING);  //创建第一个单元格
        cell_1.setCellValue("学号");  //为第一个单元格赋值
        Cell cell_2 = row0.createCell(1, Cell.CELL_TYPE_STRING);
        cell_2.setCellValue("姓名");
        Cell cell_3 = row0.createCell(2, Cell.CELL_TYPE_STRING);
        cell_3.setCellValue("正确数");
        System.out.println("请将学生答题卡文件夹路径拖动到下一行，然后按回车....");
        Scanner scanner3 = new Scanner(System.in);
        String AnswerPath = scanner3.nextLine();
        File file = new File(AnswerPath);
        File flist[] = file.listFiles();
        List<List<String>> aai=null;
        int id = 1;
        for (File f : flist) {        //遍历文件夹下的文件
            aai= poi.read(f.getAbsolutePath());
            List<String> stuinfo = new ArrayList<String>();
            List<String> stu_ans = new ArrayList<String>();
            int count = 0;
            if (list != null) {
                for (int i = 0; i < 5; i++) {                    //学生信息为1-5行
                    List<String> cellList2 = aai.get(i);
                    for (int j = 1; j < 2; j++) {                //第一行第二个单元格和第二行第二个单元格
                        stuinfo.add(cellList2.get(1));
                    }
                }

                for (int i = 5; i <aai.size(); i++) {          //学生答案为4-24行 ，第三行为空行
                    List<String> cellList = aai.get(i);
                    for (int j = 1; j < 2; j++) {               //第三行第二个的单元格
                        stu_ans.add(cellList.get(1));              //获取学生答案
                        if (cellList.get(1).equals(right_ans.get(i-5))) {    //如果和标准答案匹配，count++
                            count++;
                        }
                    }
                }
            }
            Row row = sheet.createRow(id);
            Cell cell1 = row.createCell(0, Cell.CELL_TYPE_STRING);
            Cell cell2 = row.createCell(1, Cell.CELL_TYPE_STRING);
            Cell cell3 = row.createCell(2, Cell.CELL_TYPE_STRING);
            cell1.setCellValue(stuinfo.get(2));
            cell2.setCellValue(stuinfo.get(0));
            cell3.setCellValue(count-5);
            id++;
            FileOutputStream outputStream = new FileOutputStream(excelPath);
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            System.out.println(stuinfo.get(0) + "交卷，正确数：" + (count-5));  //输出正确题目数
        }
        percentage p=new percentage();
        p.calc(KeyPath,AnswerPath,a,aai.size());
        System.out.println("阅卷完毕,共" + flist.length + "个学生交卷，正在生成阅卷报告....");
    }
}
